﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;

namespace NetMicro.Cache.RedisProvider
{
    public partial class CsRedisCache : IRedisCache
    {

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">缓存Value</param>
        /// <param name="entry">缓存策略</param>
        /// <returns></returns>
        public void Set<T>(string key, T value, CacheEntry entry)
        {
            DateTimeOffset? timeOffset = null;
            if (entry.AbsoluteExpiration.Ticks == 0 && entry.SlidingExpiration != null)
            {
                entry.AbsoluteExpiration = (TimeSpan)entry.SlidingExpiration;
                timeOffset = DateTimeOffset.Now.Add(entry.AbsoluteExpiration);
            }
            _distributed.Set(key, _serializer.Serialize(value), new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = timeOffset,
                SlidingExpiration = entry.SlidingExpiration,
                AbsoluteExpirationRelativeToNow = entry.AbsoluteExpirationRelativeToNow
            });
        }


        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">缓存Value</param>
        /// <param name="entry">缓存策略</param>
        /// <returns></returns>
        public Task SetAsync<T>(string key, T value, CacheEntry entry)
        {
            DateTimeOffset? timeOffset = null;
            if (entry.AbsoluteExpiration.Ticks == 0 && entry.SlidingExpiration != null)
            {
                entry.AbsoluteExpiration = (TimeSpan)entry.SlidingExpiration;
                timeOffset = DateTimeOffset.Now.Add(entry.AbsoluteExpiration);
            }
            _distributed.SetAsync(key, _serializer.Serialize(value), new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = timeOffset,
                SlidingExpiration = entry.SlidingExpiration,
                AbsoluteExpirationRelativeToNow = entry.AbsoluteExpirationRelativeToNow
            });
            return Task.CompletedTask;
        }

        /// <summary>
        /// 获取滑动过期值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>

        public T GetAndRefresh<T>(string key)
        {
            var result = _distributed.Get(key);
            if (result == null)
                return default(T);
            return _serializer.Deserialize<T>(result);
        }

        /// <summary>
        /// 获取滑动过期值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<T> GetAndRefreshAsync<T>(string key)
        {
            var result = await _distributed.GetAsync(key);
            if (result == null)
                return default(T);
            return _serializer.Deserialize<T>(result);
        }
    }
}
